package com.rs.utils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.CopyOnWriteArrayList;

import com.rs.Settings;
import com.rs.cache.loaders.ItemDefinitions;
import com.rs.game.item.Item;
import com.rs.game.player.Player;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.net.ServerChannelHandler;

public final class Logger {

    private static BufferedWriter globallogs;

    static {
	try {
	    if (!Settings.DEBUG) {
		Calendar c = Calendar.getInstance();
		globallogs = new BufferedWriter(new FileWriter("data/logs/global/log." + ((c.get(Calendar.MONTH)) + 1) + "." + c.get(Calendar.DATE) + "." + c.get(Calendar.YEAR) + ".txt", true));
	    }
	}
	catch (Throwable e) {
	    Logger.handle(e);
	}
    }

	public static void handle(Throwable throwable) {
		System.out.println("ERROR! THREAD NAME: "
				+ Thread.currentThread().getName());
		throwable.printStackTrace();
	}

	public static void debug(long processTime) {
		log(Logger.class, "---DEBUG--- start");
		log(Logger.class, "WorldProcessTime: " + processTime);
		log(Logger.class,
				"WorldRunningTasks: " + WorldTasksManager.getTasksCount());
		log(Logger.class,
				"ConnectedChannels: "
						+ ServerChannelHandler.getConnectedChannelsSize());
		log(Logger.class, "---DEBUG--- end");
	}

	public static void log(Object classInstance, Object message) {
		log(classInstance.getClass().getSimpleName(), message);
	}

	public static void log(String className, Object message) {
		Date time = Calendar.getInstance().getTime();
		String text = "[" + time.toString() + "]" + " " + message.toString();
		System.err.println(text);
	}
	
	public static void log(Object message) {
		Date time = Calendar.getInstance().getTime();
		String text = "[" + time.toString() + "]" + " " + message.toString();
		System.err.println(text);
	}

	private Logger() {

	}
	   public static void logLostDuelStake(Player loser, Player victor,
			    CopyOnWriteArrayList<Item> lostItems) {
			// TODO Auto-generated method stub
			try {
			    String FILE_PATH = Settings.LOGS_PATH + "/stakelogs/";
			    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
			    Calendar cal = Calendar.getInstance();
			    BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH
				    + loser.getUsername() + ".txt", true));
			    writer.write("[Stake session started]");
			    writer.newLine();
			    writer.write("Stake Winner Information: Username: "
				    + victor.getUsername() + ". IP "
				    + victor.getSession().getIP() + ". Current Mac: "
				    + victor.getCurrentMac() + ". Location: " + victor.getX()
				    + ", " + victor.getY() + ", " + victor.getPlane() + ".");
			    writer.newLine();
			    writer.write("Stake Loser Information: Username: "
				    + loser.getUsername() + ". IP: " + loser.getLastIP()
				    + ". Current Mac: " + loser.getCurrentMac()
				    + ". Location: " + loser.getX() + ", " + loser.getY()
				    + ", " + loser.getPlane() + ".");
			    writer.newLine();
			    writer.write("Time: [" + dateFormat.format(cal.getTime()) + "]");
			    for (Item item : lostItems) {
				if (item == null)
				    continue;
				ItemDefinitions defs = ItemDefinitions.getItemDefinitions(item
					.getId());
				String name = defs == null ? "" : defs.getName().toLowerCase();
				writer.newLine();
				writer.write(loser.getUsername() + " lost: " + name
					+ ", amount: " + item.getAmount() + " to "
					+ victor.getUsername() + ".");
			    }
			    writer.newLine();
			    writer.write("[Stake session ended]");
			    writer.newLine();
			    writer.flush();
			    writer.close();
			} catch (IOException e) {
			    Logger.log(loser, e);
			}
		    }

	  public static void logDuelStake(Player player, Player victor,
			    CopyOnWriteArrayList<Item> wonItems) {
			// TODO Auto-generated method stub
			try {
			    String FILE_PATH = Settings.LOGS_PATH + "/stakelogs/";
			    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
			    Calendar cal = Calendar.getInstance();
			    BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH
				    + victor.getUsername() + ".txt", true));
			    writer.write("[Stake session started]");
			    writer.newLine();
			    writer.write("Stake Winner Information: Username: "
				    + victor.getUsername() + ". IP "
				    + victor.getSession().getIP() + ". Current Mac: "
				    + victor.getCurrentMac() + ". Location: " + victor.getX()
				    + ", " + victor.getY() + ", " + victor.getPlane() + ".");
			    writer.newLine();
			    writer.write("Stake Loser Information: Username: "
				    + player.getUsername() + ". IP: " + player.getLastIP()
				    + ". Current Mac: " + player.getCurrentMac()
				    + ". Location: " + player.getX() + ", " + player.getY()
				    + ", " + player.getPlane() + ".");
			    writer.newLine();
			    writer.write("Time: [" + dateFormat.format(cal.getTime()) + "]");
			    for (Item item : wonItems) {
				if (item == null)
				    continue;
				ItemDefinitions defs = ItemDefinitions.getItemDefinitions(item
					.getId());
				String name = defs == null ? "" : defs.getName().toLowerCase();
				writer.newLine();
				writer.write(victor.getUsername() + " won: " + name
					+ ", amount: " + item.getAmount() + " from "
					+ player.getUsername() + ".");
			    }
			    writer.newLine();
			    writer.write("[Stake session ended]");
			    writer.newLine();
			    writer.flush();
			    writer.close();
			} catch (IOException e) {
			    Logger.log(player, e);
			}
		    }

    public static void globalLog(String ip, String name, Object o) {
	try {
	    if (!Settings.DEBUG) {
		globallogs.write("[" + Utils.currentTime("hh:mm:ss z") + "]" + "" + name + " - " + ip + " [ " + o + " ]");
		globallogs.newLine();
		globallogs.flush();
	    }
	}
	catch (IOException e) {
	    e.printStackTrace();
	}
    }
}
